package edu.hrbeu.crc.handler;

import edu.hrbeu.crc.constant.ErrorConstant;
import edu.hrbeu.crc.pojo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result globalExceptionHandler(Exception e) {
        String msg = e.getMessage();
        log.error(msg, e);
        if (ErrorConstant.isValidErrorMsg(msg)) {
            return Result.error(msg);
        }else {
            return Result.error("未知错误，请联系管理员");
        }
    }
}
